"use strict";
const path = require("path");
const defaultSettings = require("./src/settings.js");

function resolve(dir) {
    return path.join(__dirname, dir);
}

const name = defaultSettings.title || "vue Element Admin"; // page title

const port = process.env.port || process.env.npm_config_port || 9900; // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
    /**
     * You will need to set publicPath if you plan to deploy your site under a sub path,
     * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
     * then publicPath should be set to "/bar/".
     * In most cases please use '/' !!!
     * Detail: https://cli.vuejs.org/config/#publicpath
     */
    publicPath: "/",
    outputDir: "dist",
    assetsDir: "./admin/static",
    lintOnSave: process.env.NODE_ENV === "development",
    productionSourceMap: false,
    devServer: {
        port: port,
        open: true,
        overlay: {
            warnings: false,
            errors: true
        },
        // 15086868745   1887022998893314048
        // 206b7617c42771ef801b5017f0e90102
        // 605a7e17c42771ef9a5b5017e1f90102

        // 18980363765  1945666993210621952
        // 1888395621636194304,1888395775034474496,1888395963446804480,1888421586324111360,1888421680981164032,1888422535100841984,1888423388213886976,1888423300863311872,1888423177680797696,1888397821313761280,1888397926750175232,1888398019414933504
        // column    1904000401767088128   1888423177680797696



        // 18370212590
        https: false,
        hotOnly: false,
        proxy: {
            // 配置跨域
            "/api/yuntk": {
                // target: "https://api-pro.yunxuebao.com",
                target: "http://127.0.0.1:26001",
                ws: true,
                changOrigin: true,
                pathRewrite: {
                    "^/api/yuntk": ""
                }
            },
            "/cdn/vers7": {
                target: "https://vipcdn.langrundata.com",
                ws: true,
                changOrigin: true,
                pathRewrite: {
                    "": ""
                }
            },
        }
    },
    configureWebpack: {
        name: name,
        resolve: {
            alias: {
                "@": resolve("src")
            }
        }
    },
    chainWebpack(config) {
        config.plugins.delete("prefetch");
        config.module
            .rule("svg")
            .exclude.add(resolve("src/icons"))
            .end();
        config.module
            .rule("icons")
            .test(/\.svg$/)
            .include.add(resolve("src/icons"))
            .end()
            .use("svg-sprite-loader")
            .loader("svg-sprite-loader")
            .options({
                symbolId: "icon-[name]"
            })
            .end();
        config.module
            .rule("vue")
            .use("vue-loader")
            .loader("vue-loader")
            .tap(options => {
                options.compilerOptions.preserveWhitespace = true;
                return options;
            })
            .end();

        config.when(process.env.NODE_ENV !== "development", config => {
            config
                .plugin("ScriptExtHtmlWebpackPlugin")
                .after("html")
                .use("script-ext-html-webpack-plugin", [{
                    inline: /runtime\..*\.js$/
                }])
                .end();
            config.optimization.splitChunks({
                chunks: "all",
                cacheGroups: {
                    libs: {
                        name: "chunk-libs",
                        test: /[\\/]node_modules[\\/]/,
                        priority: 10,
                        chunks: "initial"
                    },
                    elementUI: {
                        name: "chunk-elementUI",
                        priority: 20,
                        test: /[\\/]node_modules[\\/]_?element-ui(.*)/
                    },
                    commons: {
                        name: "chunk-commons",
                        test: resolve("src/components"),
                        minChunks: 3,
                        priority: 5,
                        reuseExistingChunk: true
                    }
                }
            });
            config.optimization.runtimeChunk("single");
        });
    }
};
